package pcserver;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import drummer.server.Command;
import drummer.server.CommandHandler;



public class MainFrame extends JFrame{

	private static final long serialVersionUID = 1L;
	
	private StartWindow startWindow;
	private HelpWindow helpWindow;
	private GameWindow gameWindow;
	
	private JLabel background;
	private AbstractWindow currentWindow;
	private BackMusic bgm;
	private BackMusic gamebgm;
	private BackMusic se;
	private ReceiverHandler receiverHandler;
	
	public MainFrame(){
		bgm = new BackMusic();
		gamebgm = new BackMusic();
		se = new BackMusic();
		bgm.setMusic(util.music + "start.wav");
		gamebgm.setMusic(util.music + "default.wav");
		se.setMusic(util.music + "hint.wav");
		
		startWindow = new StartWindow();
		startWindow.getStart().addActionListener(new StartListener());
		startWindow.getHelp().addActionListener(new HelpListener());
		startWindow.getExit().addActionListener(new ExitListener());
		helpWindow = new HelpWindow();
		helpWindow.getBack().addActionListener(new HelpBackListener());		
		
		background = new JLabel();
		((JPanel) this.getContentPane()).setOpaque(false);
		this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
		background.setBounds(0, 0, 800, 600); 
		
		add(startWindow);
		currentWindow = startWindow;
		setBackground();
		
		this.addKeyListener(new GameKeyListener());
		receiverHandler = new ReceiverHandler();
		
		setTitle("太鼓达人");
		setSize(800, 600);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}
	
	private void setBackground(){
		background.setIcon(currentWindow.getBgimage());
		if(currentWindow instanceof StartWindow && !bgm.isPlay()){
			bgm.loop();
		}
		else if(currentWindow instanceof GameWindow){	
			gamebgm.play();
		}
	}
	
	public void setReceiverHandler(ReceiverHandler receiverHandler) {
		this.receiverHandler = receiverHandler;
		
	}

	public ReceiverHandler getReceiverHandler() {
		JOptionPane.showMessageDialog(startWindow, "链接已建立", "链接已建立", JOptionPane.INFORMATION_MESSAGE);
		return receiverHandler;
	}

	class ReceiverHandler implements CommandHandler{
		public void handleCommand(Command command) {
			// TODO Auto-generated method stub
			if(command.getType() == 1 && currentWindow instanceof GameWindow){
				
				double x = command.getX();
				System.out.print("xxx:" + x +";");
				if(x < -5){
					
					System.out.print("yyy:");
					gameWindow.hintdown();
					se.play();
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					gameWindow.hintup();
				}
			}
			
		}
	}
	class GameKeyListener implements KeyListener{

		@Override
		public void keyTyped(KeyEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void keyPressed(KeyEvent e) {
			// TODO Auto-generated method stub
			if(currentWindow == gameWindow && gameWindow.getStatus() == 0){
				//System.out.println("down");
				if (e.getKeyCode() == KeyEvent.VK_DOWN){
					gameWindow.hintdown();
					//se.stop();
					se.play();
				}
			}
			
		}

		@Override
		public void keyReleased(KeyEvent e) {
			// TODO Auto-generated method stub
			if(currentWindow == gameWindow && gameWindow.getStatus() == 1){
				//System.out.println("up");
				if (e.getKeyCode() == KeyEvent.VK_DOWN){
					gameWindow.hintup();
				}
			}
		}
	
	}
	
	class StartListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			bgm.stop();
			startWindow.setVisible(false);
			gameWindow = new GameWindow(new GameData("src/data/default.txt"));
			gameWindow.getBack().addActionListener(new GameBackListener());	
			gameWindow.setVisible(true);
			add(gameWindow);
			currentWindow = gameWindow;	
			setBackground();
			gameWindow.start();
		}
		
	}
	
	class HelpListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			startWindow.setVisible(false);
			helpWindow.setVisible(true);
			add(helpWindow);
			currentWindow = helpWindow;	
			setBackground();
		}
		
	}
	
	class HelpBackListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			helpWindow.setVisible(false);
			startWindow.setVisible(true);
			add(startWindow);
			currentWindow = startWindow;	
			setBackground();
		}
		
	}
	
	class GameBackListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			//gameWindow.setVisible(false);
			gameWindow.stop();
			remove(gameWindow);
			gamebgm.stop();
			startWindow.setVisible(true);
			add(startWindow);
			currentWindow = startWindow;	
			setBackground();
		}
		
	}
	
	class ExitListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			System.exit(0);
		}
		
	}

	/*public static void main(String[] args) {
		MainFrame frame = new MainFrame();	
		frame.setLocation(100, 100);
	}*/
	
}
